#pragma once

class CommandSettings
{
private:
	ATL::CStringW _text;
	DWORD _flags;

	enum {
		CF_Initialized = 1,
		CF_UseSendMessage = 2,
		CF_ParseText = 4,
	};

public:

	CommandSettings() : _flags(CF_UseSendMessage)
	{
	}

	bool Load(p_command command)
	{
		if(command == NULL)
			return false;

		EnterCriticalSection(&command->critical_section);

		_text.Empty();
		if(command->binary != NULL && command->size > 0)
			_text.Append(static_cast<wchar_t*>(command->binary), command->size / 2);

		if((DWORD(command->lvalue1) & CF_Initialized) != 0)
			_flags = DWORD(command->lvalue1);

		_flags |= CF_Initialized;

		LeaveCriticalSection(&command->critical_section);
		return true;
	}

	bool Save(p_command command)
	{
		if(command == NULL)
			return false;

		EnterCriticalSection(&command->critical_section);

		if(command->binary != NULL)
		{
			_GirderAPI.gir_free(command->binary);
			command->binary = NULL;
			command->size = 0;
		}

		command->size = int(_text.GetLength() * sizeof(wchar_t));
		command->binary = _GirderAPI.gir_malloc(command->size);

		if(command->binary != NULL)
			memcpy_s(command->binary, command->size, static_cast<wchar_t*>(_text.GetBuffer()), _text.GetLength() * sizeof(wchar_t));
		else
			command->size = 0;

		command->lvalue1 = int(_flags);

		command->actiontype = SetWinTextPlugin::PLUGIN_ID;
		_GirderAPI.set_command(command);

		LeaveCriticalSection(&command->critical_section);
		
		return true;
	}

	ATL::CString& GetText()
	{
		return _text;
	}

	void SetText(const ATL::CString& value)
	{
		_text = value;
	}

	bool GetUseSendMessage() const
	{
		return (_flags & CF_UseSendMessage) != 0;
	}
	void SetUseSendMessage(bool value)
	{
		if(value)
			_flags |= CF_UseSendMessage;
		else
			_flags &= ~CF_UseSendMessage;
	}
	
	bool GetParseText() const
	{
		return (_flags & CF_ParseText) != 0;
	}
	void SetParseText(bool value)
	{
		if(value)
			_flags |= CF_ParseText;
		else
			_flags &= ~CF_ParseText;
	}
	

	// Properties
	__declspec(property(get = GetUseSendMessage, put = SetUseSendMessage)) bool UseSendMessage;
	__declspec(property(get = GetParseText, put = SetParseText)) bool ParseText;
	__declspec(property(get = GetText, put = SetText)) ATL::CString& Text;
};


